//
//  BSBTabBarControllerViewController.m
//  Baisebudejie
//
//  Created by apple on 16/3/14.
//  Copyright © 2016年 youminuo. All rights reserved.
//

#import "BSBTabBarController.h"
#import "BSBNavigationController.h"
#import "BSBEssenceController.h"
#import "BSBNewController.h"
#import "BSBFriendController.h"
#import "BSBMineController.h"
#import "BSBTabbar.h"
#import "BSBModelView.h"

@interface BSBTabBarController ()
@property (strong, nonatomic) NSMutableArray *controllerArray;
@end

@implementation BSBTabBarController


// 控制器数组懒加载
- (NSMutableArray *)controllerArray
{
    if (_controllerArray == nil)
    {
        _controllerArray = [NSMutableArray array];
    }
    return _controllerArray;
}

//设置tabbar的文字属性
+ (void)initialize
{
    UITabBarItem *bar = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];
    NSDictionary *noldict = @{NSForegroundColorAttributeName : [UIColor grayColor]};
    NSDictionary *seledict = @{NSForegroundColorAttributeName : [UIColor darkGrayColor]};
    [bar setTitleTextAttributes:noldict forState:UIControlStateNormal];
    [bar setTitleTextAttributes:seledict forState:UIControlStateSelected];
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    加载自控制器
    [self setUpchildView];
    self.viewControllers = self.controllerArray;
//    自定义底部tabbar
    BSBTabbar *bar = [[BSBTabbar alloc] init];
    bar.modelBlock = ^{
        BSBModelView *modelView = [[BSBModelView alloc] init];
        [self presentViewController:modelView animated:YES completion:nil];
        
    };
    [self setValue:bar forKeyPath:@"tabBar"];
}

#pragma mark - 创建控制器
- (void)setUpchildView
{
//    全部
    BSBEssenceController *esseVC = [[BSBEssenceController alloc] init];
    [self setUpcontr:esseVC title:@"全部" image:@"tabBar_essence_icon" seleimage:@"tabBar_essence_click_icon"];
    
//    新的
    BSBNewController *newVC = [[BSBNewController alloc] init];
    [self setUpcontr:newVC title:@"新的" image:@"tabBar_new_icon" seleimage:@"tabBar_new_click_icon"];
    
//    关注
    BSBFriendController *friendVC = [[BSBFriendController alloc] init];
    [self setUpcontr:friendVC title:@"关注" image:@"tabBar_friendTrends_icon" seleimage:@"tabBar_friendTrends_click_icon"];
    
//    我的
    BSBMineController *mineVC = [[BSBMineController alloc] init];
    [self setUpcontr:mineVC title:@"我的" image:@"tabBar_me_icon" seleimage:@"tabBar_me_click_icon"];
    
}

#pragma mark - 包装成导航控制器
- (void)setUpcontr:(UIViewController *)controller title:(NSString *)title image:(NSString *)image seleimage:(NSString *)seleimage
{
//    设置图片渲染模式为本身颜色
    UIImage *nolimage = [UIImage imageNamed:image];
    nolimage = [nolimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIImage *selimage = [UIImage imageNamed:seleimage];
    selimage = [selimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    添加导航控制器
    controller.tabBarItem.title = title;
    controller.tabBarItem.image = nolimage;
    controller.tabBarItem.selectedImage = selimage;
    BSBNavigationController *navg = [[BSBNavigationController alloc] initWithRootViewController:controller];
    [self.controllerArray addObject:navg];
}



@end
